<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java 常量池深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .feature-icon {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        
        .code-snippet {
            background: #2d3748;
            color: #e2e8f0;
            padding: 2px 8px;
            border-radius: 4px;
            font-family: 'Courier New', monospace;
            font-size: 0.9em;
        }
        
        .first-letter {
            float: left;
            font-size: 3.5em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fadeInUp {
            animation: fadeInUp 0.6s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center animate-fadeInUp">
            <h1 class="text-5xl md:text-6xl font-bold mb-6" style="font-family: 'Noto Serif SC', serif;">
                Java 常量池
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">
                深入理解 JVM 内存优化的核心机制
            </p>
            <div class="flex justify-center space-x-8 text-lg">
                <div class="flex items-center">
                    <i class="fas fa-memory mr-2"></i>
                    <span>内存优化</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-tachometer-alt mr-2"></i>
                    <span>性能提升</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-code mr-2"></i>
                    <span>代码效率</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8 animate-fadeInUp">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="first-letter">在</span>Java 中，<strong class="text-purple-700">常量池</strong>是一个重要的概念，它用于提高程序的性能和节省内存。常量池主要包括两个部分：<strong class="text-purple-700">字符串常量池</strong>和<strong class="text-purple-700">类常量池</strong>。它不仅节省了内存，还提高了程序的执行效率。
            </p>
        </div>

        <!-- Visualization -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8 animate-fadeInUp" style="animation-delay: 0.1s;">
            <h2 class="text-2xl font-bold mb-6 text-center text-gray-800">常量池架构图</h2>
            <div class="mermaid">
                graph TB
                    A[Java 常量池] --> B[字符串常量池]
                    A --> C[类常量池]
                    B --> D[字面量字符串]
                    B --> E[intern() 方法]
                    C --> F[文字常量]
                    C --> G[符号引用]
                    D --> H[内存去重]
                    E --> I[运行时加入]
                    F --> J[整型/浮点型]
                    G --> K[方法名/字段名/类名]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style B fill:#9f7aea,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#9f7aea,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Basic Concept -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8 animate-fadeInUp" style="animation-delay: 0.2s;">
            <h3 class="section-title text-2xl font-bold mb-6 text-gray-800">
                <i class="fas fa-lightbulb feature-icon mr-3"></i>常量池的基本概念
            </h3>
            <p class="text-lg leading-relaxed text-gray-700 pl-8">
                <strong class="text-purple-700">常量池</strong>是 JVM（Java Virtual Machine）中一种特殊的数据结构，用于存储程序中的常量值。它的目的是避免重复存储相同的常量，从而节省内存并提高程序的执行效率。
            </p>
        </div>

        <!-- String Constant Pool -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8 animate-fadeInUp" style="animation-delay: 0.3s;">
            <h3 class="section-title text-2xl font-bold mb-6 text-gray-800">
                <i class="fas fa-quote-left feature-icon mr-3"></i>字符串常量池
            </h3>
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                <strong class="text-purple-700">字符串常量池</strong>是常量池的一个重要部分，用于存储在程序中使用的字符串常量。具体实现如下：
            </p>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                    <div class="text-3xl mb-4 text-purple-600">
                        <i class="fas fa-database"></i>
                    </div>
                    <h4 class="font-bold text-lg mb-2 text-gray-800">存储机制</h4>
                    <p class="text-gray-700">当你在 Java 程序中使用字面量字符串（如 <span class="code-snippet">"hello"</span>）时，这些字符串会被存储在一个特定的内存区域，称为字符串常量池。这个池子是由 JVM 管理的。</p>
                </div>
                
                <div class="bg-gradient-to-br from-blue-50 to-indigo-50 p-6 rounded-xl">
                    <div class="text-3xl mb-4 text-blue-600">
                        <i class="fas fa-recycle"></i>
                    </div>
                    <h4 class="font-bold text-lg mb-2 text-gray-800">去重和重用</h4>
                    <p class="text-gray-700">如果在程序中多次使用相同的字符串字面量，JVM 会确保这些字符串只在常量池中存储一次，而不是每次出现时都创建新的字符串对象。</p>
                </div>
                
                <div class="bg-gradient-to-br from-green-50 to-teal-50 p-6 rounded-xl">
                    <div class="text-3xl mb-4 text-green-600">
                        <i class="fas fa-cogs"></i>
                    </div>
                    <h4 class="font-bold text-lg mb-2 text-gray-800">实现方法</h4>
                    <p class="text-gray-700">当创建字符串对象时，JVM 首先会检查常量池中是否已经存在相同的字符串。如果存在，则直接返回池中的字符串引用；如果不存在，则将新的字符串添加到池中。</p>
                </div>
            </div>
        </div>

        <!-- Class Constant Pool -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8 animate-fadeInUp" style="animation-delay: 0.4s;">
            <h3 class="section-title text-2xl font-bold mb-6 text-gray-800">
                <i class="fas fa-layer-group feature-icon mr-3"></i>类常量池
            </h3>
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                <strong class="text-purple-700">类常量池</strong>是指每个 Java 类或接口都有一个常量池，用于存储类中使用的各种常量值和符号引用。这些常量值包括：
            </p>
            
            <div class="flex flex-col md:flex-row gap-6">
                <div class="flex-1 bg-gradient-to-r from-purple-100 to-purple-50 p-6 rounded-xl border-l-4 border-purple-500">
                    <h4 class="font-bold text-lg mb-2 text-gray-800 flex items-center">
                        <i class="fas fa-file-alt mr-2 text-purple-600"></i>文字常量
                    </h4>
                    <p class="text-gray-700">如整型、浮点型常量等。</p>
                </div>
                
                <div class="flex-1 bg-gradient-to-r from-blue-100 to-blue-50 p-6 rounded-xl border-l-4 border-blue-500">
                    <h4 class="font-bold text-lg mb-2 text-gray-800 flex items-center">
                        <i class="fas fa-link mr-2 text-blue-600"></i>符号引用
                    </h4>
                    <p class="text-gray-700">如方法名、字段名、类名等，用于在运行时通过反射、方法调用等机制进行访问。</p>
                </div>
            </div>
            
            <p class="text-lg leading-relaxed text-gray-700 mt-6">
                <strong class="text-purple-700">类常量池</strong>在类加载时由 JVM 解析和处理，并在运行时为访问常量提供高效的机制。
            </p>
        </div>

        <!-- Functions -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8 animate-fadeInUp" style="animation-delay: 0.5s;">
            <h3 class="section-title text-2xl font-bold mb-6 text-gray-800">
                <i class="fas fa-rocket feature-icon mr-3"></i>常量池的作用
            </h3>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="text-center p-6 hover:transform hover:scale-105 transition-transform duration-300">
                    <div class="text-5xl mb-4 feature-icon">
                        <i class="fas fa-compress-alt"></i>
                    </div>
                    <h4 class="font-bold text-xl mb-2 text-gray-800">节省内存</h4>
                    <p class="text-gray-600">通过将重复的常量存储在常量池中，JVM 可以有效地减少内存的使用。</p>
                </div>
                
                <div class="text-center p-6 hover:transform hover:scale-105 transition-transform duration-300">
                    <div class="text-5xl mb-4 feature-icon">
                        <i class="fas fa-bolt"></i>
                    </div>
                    <h4 class="font-bold text-xl mb-2 text-gray-800">提高性能</h4>
                    <p class="text-gray-600">访问常量池中的常量比创建新的对象更高效，尤其是在处理大量重复的常量时。</p>
                </div>
                
                <div class="text-center p-6 hover:transform hover:scale-105 transition-transform duration-300">
                    <div class="text-5xl mb-4 